{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "51ea86e0-7a92-4bb6-b7e6-c59d33b2c6d5",
   "metadata": {},
   "outputs": [],
   "source": [
    "import pickle\n",
    "d = dict(name='Bob', age = 20, score=90)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "bd7161c6-05d5-4743-a2a3-7da4cfde0003",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "b'\\x80\\x04\\x95$\\x00\\x00\\x00\\x00\\x00\\x00\\x00}\\x94(\\x8c\\x04name\\x94\\x8c\\x03Bob\\x94\\x8c\\x03age\\x94K\\x14\\x8c\\x05score\\x94KZu.'"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "pickle.dumps(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "9386b588-c842-4d7a-a4f0-abb42a186f01",
   "metadata": {},
   "outputs": [],
   "source": [
    "f = open('dump.txt', 'wb')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "0e532248-d435-4772-b5c9-ec087d4c0a2e",
   "metadata": {},
   "outputs": [],
   "source": [
    "pickle.dump(d, f)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "7a0aabb7-42bc-43a2-94d2-5236f3d8315b",
   "metadata": {},
   "outputs": [],
   "source": [
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "85340534-b3a1-497c-ba4b-3c2e3d8d68d3",
   "metadata": {},
   "outputs": [],
   "source": [
    "f = open('dump.txt', 'rb')\n",
    "d = pickle.load(f)\n",
    "f.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "acc98223-673a-47c8-925b-60b6d00c6a45",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': 'Bob', 'age': 20, 'score': 90}"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "d"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "a34906d2-7d33-4cb8-9256-72bc76eb5cad",
   "metadata": {},
   "outputs": [],
   "source": [
    "import json\n",
    "d = dict(name='Bob', age = 20, score=90)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "a248ab68-3804-4251-b892-319d098ce695",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'{\"name\": \"Bob\", \"age\": 20, \"score\": 90}'"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "json.dumps(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "76fedde6-52a5-4065-9869-5300b975019c",
   "metadata": {},
   "outputs": [],
   "source": [
    "jsonStr = '{\"name\": \"Bob\", \"age\": 20, \"score\": 90}'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "c0641828-abe2-493c-a1da-e8c0ad2898e7",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': 'Bob', 'age': 20, 'score': 90}"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "json.loads(jsonStr)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "dfaa65fd-919c-4bf6-b23d-33bad58325a2",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "713c4324-75e2-44d8-8e36-b22848d7d18d",
   "metadata": {},
   "outputs": [],
   "source": [
    "class Student(object):\n",
    "\n",
    "    def __init__(self, name, age, score):\n",
    "        self.name = name\n",
    "        self.age = age\n",
    "        self.score = score\n",
    "\n",
    "    def __str__(self):\n",
    "        return \"Student object (%s, %s, %s)\" % (self.name, self.age, self.score)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "ee5a07d6-a61a-4d78-87c0-c234ed93dade",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['__class__',\n",
       " '__delattr__',\n",
       " '__dict__',\n",
       " '__dir__',\n",
       " '__doc__',\n",
       " '__eq__',\n",
       " '__format__',\n",
       " '__ge__',\n",
       " '__getattribute__',\n",
       " '__gt__',\n",
       " '__hash__',\n",
       " '__init__',\n",
       " '__init_subclass__',\n",
       " '__le__',\n",
       " '__lt__',\n",
       " '__module__',\n",
       " '__ne__',\n",
       " '__new__',\n",
       " '__reduce__',\n",
       " '__reduce_ex__',\n",
       " '__repr__',\n",
       " '__setattr__',\n",
       " '__sizeof__',\n",
       " '__str__',\n",
       " '__subclasshook__',\n",
       " '__weakref__',\n",
       " 'age',\n",
       " 'name',\n",
       " 'score']"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = Student(\"Bob\", 20, 88)\n",
    "dir(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "dd137b9e-f5c0-47c8-a0fe-cbfcd70a2804",
   "metadata": {},
   "outputs": [],
   "source": [
    "std_data = json.dumps(s, default=lambda obj:obj.__dict__ )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "02c0f8b1-6377-4cb7-835f-026b1317b4ef",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dump student:  {\"name\": \"Bob\", \"age\": 20, \"score\": 88}\n"
     ]
    }
   ],
   "source": [
    "print(\"dump student: \", std_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "7be2885b-96a4-48c5-a3d4-9586913b0d52",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': 'Bob', 'age': 20, 'score': 88}"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s.__dict__"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "98811e93-0699-4ce1-97a6-e1576b99edd3",
   "metadata": {},
   "outputs": [],
   "source": [
    "def dict2student(d):\n",
    "    return Student(d['name'], d['age'], d['score'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "eda65b67-6b67-4e6f-900e-23ed3623ec9a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Student object (Bob, 20, 88)\n"
     ]
    }
   ],
   "source": [
    " json_str = '{\"age\": 20, \"score\": 88, \"name\": \"Bob\"}'\n",
    "print(json.loads(json_str, object_hook=dict2student))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "cff27457-458a-4452-adcc-4fa639aafe3a",
   "metadata": {},
   "outputs": [],
   "source": [
    "\n",
    "obj = dict(name='小明', age=20)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "ad0cf41f-1180-4eb7-8da8-191eb055a501",
   "metadata": {},
   "outputs": [],
   "source": [
    "s = json.dumps(obj, ensure_ascii=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "1fb3add8-06f2-46f9-859a-5abe48a6d741",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'{\"name\": \"\\\\u5c0f\\\\u660e\", \"age\": 20}'"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0a615944-b369-4256-93d9-d8c930ddbe4a",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.21"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
